 <?php
 // === Autoloader laden und konfigurieren ===
 include_once('autoloader.php');



Class Index_RegisterView Extends Yapary_MVC_View
{
	const NEWLINE = "\n";


    public function defaultView (array $arrVar)
    {
    	$output		= '';

    	// Auszugebende Daten im entsprechenden Registry-Eintrag speichern
    	$registry = Yapary_SingletonRegistry::getInstance();

    	/* @var $oSql Yapary_Db_Sql_Connector */
    	$oSql = $registry->oSql;
    	/* @var $model Index_RegisterModel */
    	$model = $this->model; // Durch @var-Definition Eclipse Autocomplete nutzen!
    	/* @var $oHeader Yapary_Header_Header */
    	$oHeader = $registry->oHeader;
    	/* @var $oMetaTag Yapary_Header_MetaTag */
    	$oMetaTag = $registry->oMetaTag;
    	/* @var $oMainMenu Yapary_Menu_XmlMenu */
    	$oMainMenu = $registry->oMainMenu;
    	/* @var $oFooterMenu Yapary_Menu_XmlMenu */
    	$oFooterMenu = $registry->oFooterMenu;
    	/* @var $oXmlTranslator Yapary_Translation_XmlTranslator */
    	$oXmlTranslator = $registry->oXmlTranslator;

    	/* @var $oManager UserManager */
    	$oManager = new UserManager();


    	// HTML Head Title
    	$oHeader->setTitle('Registrierungsvorgang');
    	$oHeader->addCssFile('frontend/style/register.css');

    	// MainMenu
		$oMainMenu->expand('ACCOUNT_INDEX');
		$oMainMenu->setSelected(strtoupper(Yapary_SingletonRegistry::getInstance()->routeName));
//		$oMainMenu->addAfter('BASKET_INDEX', 'INSTALL_INDEX', 'install.php', 'Install/uninstall');

		// FooterMenu
		$oFooterMenu->setSelected(strtoupper(Yapary_SingletonRegistry::getInstance()->routeName));


		// CONTENT
		$checkLoginData = $this->checkLoginData();
		if ($model->send === true && $checkLoginData == '' && $this->register() == true) { // Form Post wurde gesendet und Registrierung ist erfolgreich
			$output .= $oXmlTranslator->get('REGISTER_SUCCESS').'<br />';
			$loginErr = $oManager->loginUserByDb($model->email, $model->password);
			if ($loginErr == 1) {
				$output .= $oXmlTranslator->get('LOGGED_IN');
			} else {
				$output .= $oXmlTranslator->get('ERR_LOGGED_IN', array('VAR' => $loginErr));
			}
		} else {
			$output .= $checkLoginData;
			$output .= $this->getDialog();
		}

		// Daten in globaler Registry speichern, um diese in "layout.phtml" ausgeben zu koennen
		$registry->header		= $this->partial->insert('headerData.phtml');
		$registry->mainMenu		= $oMainMenu->display(strtoupper(Yapary_SingletonRegistry::getInstance()->routeName));
		$registry->content		= $this->partial->insert('contentData.phtml', array('content_mid' => $output));
    	$registry->footerMenu	= $oFooterMenu->display();
    }


    private function getDialog () {
    	$model = $this->model;
    	$registry = Yapary_SingletonRegistry::getInstance();
    	$oXmlTranslator = $registry->oXmlTranslator;
    	$oComponent = new Yapary_Form_Component();

    	return '<form method="post" action="">
				<table>
    			<br>
				<tr>
					<th>'.$oXmlTranslator->get('YOUR_PERSON').'</th>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('FIRSTNAME').'</th>
					<td><input type="text" name="firstName" value="'.$model->firstName.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('LASTNAME').'</th>
					<td><input type="text" name="lastName" value="'.$model->lastName.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('EMAIL').'</th>
					<td><input type="text" name="email" value="'.$model->email.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('PASSWORD').'</th>
					<td><input type="password" name="password" value="'.$model->password.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('REPEAT_PASSWORD').'</th>
					<td><input type="password" name="password2" value="'.$model->password2.'" maxlength="50" /></td>
				</tr>
				</table>

				<table>
				<tr>
					<th>'.$oXmlTranslator->get('YOUR_ADDRESS').'</th>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('COUNTRY').'</th>
					<td><select name="country">
							<option value=""></option>
							<option value="de">Deutschland</option>
							<option value="us">USA</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('ZIP').','.$oXmlTranslator->get('CITY').'</th>
					<td>
							<input type="text" name="zip" value="'.$model->zip.'" maxlength="5" />
							<input type="text" name="city" value="'.$model->city.'" maxlength="50" />
					</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('STREET').','.$oXmlTranslator->get('STREET_NUMBER').'</th>
					<td><input type="text" name="street" value="'.$model->street.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('PHONE_NUMBER').'</th>
					<td><input type="text" name="phone" value="'.$model->phone.'" maxlength="50" /></td>
				</tr>
				</table>

				<table>
				<tr>
					<th>'.$oXmlTranslator->get('USE_BILLING_ADDRESS').'</th>
					<td><input type="checkbox" name="useBillingAddress" value="'.$model->useBillingAddress.'" maxlength="50" '.$model->useBillingAddressState.' onclick="if(this.checked == true){document.getElementById(\'registerBillingDialog\').style.display=\'\';}else{document.getElementById(\'registerBillingDialog\').style.display=\'none\';}" /></td>
				</tr>
				<table>

				<table id="registerBillingDialog" style="'.$model->cssRegisterBillingDialog.'">
				<tr>
					<th>'.$oXmlTranslator->get('BILLING_ADDRESS').'</th>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('COUNTRY').'</th>
					<td><select name="countryBilling">
							<option value=""></option>
							<option value="de">Deutschland</option>
							<option value="us">USA</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('ZIP').','.$oXmlTranslator->get('CITY').'</th>
					<td>
							<input type="text" name="zipBilling" value="'.$model->zipBilling.'" maxlength="50" />
							<input type="text" name="cityBilling" value="'.$model->cityBilling.'" maxlength="50" />
					</td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('STREET').','.$oXmlTranslator->get('STREET_NUMBER').'</th>
					<td><input type="text" name="streetBilling" value="'.$model->streetBilling.'" maxlength="50" /></td>
				</tr>
				<tr>
					<th>'.$oXmlTranslator->get('PHONE_NUMBER').'</th>
					<td><input type="text" name="phoneBilling" value="'.$model->phoneBilling.'" maxlength="50" /></td>
				</tr>
				</table>

				<table>
				<tr>
					<th>&nbsp;</th>
					<td>'.$oComponent->button('register', $oXmlTranslator->get('REGISTER'), '').'</td>
				</tr>
				</table>
				</form>';

    }

    private function checkLoginData () {
    	$sendErr = NULL;
    	/* @var $model Index_RegisterModel */
    	$model = $this->model;
    	$registry = Yapary_SingletonRegistry::getInstance();
    	/* @var $oXmlTranslator Yapary_Translation_XmlTranslator */
    	$oXmlTranslator = $registry->oXmlTranslator;

    	if ($model->send === true) {
    		if ($model->firstName == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('FIRSTNAME').'</b>')).'!</li>';
    		}
    		if ($model->lastName == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('LASTNAME').'</b>')).'!</li>';
    		}
    		if ($model->email == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('EMAIL').'</b>')).'!</li>';
    		}

    		$oUserManager = new UserManager();
    		if ($oUserManager->emailExist($model->email) === true) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_EMAIL_EXISTS').'!</li>';
    		}

    		if ($model->password == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('PASSWORD').'</b>')).'!</li>';
    		}
    		if ($model->password != $model->password2) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_REPEAT_PASSWORD').'!</li>';
    		}

    		if ($model->country == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('COUNTRY').'</b>')).'!</li>';
    		}
    		if ($model->city == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('CITY').'</b>')).'!</li>';
    		}
    		if ($model->zip == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('ZIP').'</b>')).'!</li>';
    		}
    		if ($model->street == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('STREET').'</b>')).'!</li>';
    		}
    		if ($model->phone == NULL) {
    			$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('PHONE_NUMBER').'</b>')).'!</li>';
    		}

    		if ($model->useBillingAddress == true) {
    			if ($model->countryBilling == NULL) {
    				$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_BILLING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('COUNTRY').'</b>')).'!</li>';
    			}
    			if ($model->cityBilling == NULL) {
    				$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_BILLING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('CITY').'</b>')).'!</li>';
    			}
    			if ($model->zipBilling == NULL) {
    				$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_BILLING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('ZIP').'</b>')).'!</li>';
    			}
    			if ($model->streetBilling == NULL) {
    				$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_BILLING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('STREET').'</b>')).'!</li>';
    			}
    			if ($model->phoneBilling == NULL) {
    				$sendErr .= '<li>'.$oXmlTranslator->get('ERR_MISSING_BILLING_DATA', array('VAR' => '<b>'.$oXmlTranslator->get('PHONE_NUMBER').'</b>')).'!</li>';
    			}
    		}

    		if ($sendErr != NULL) {
    			return '<ul class="errorLog">'.$oXmlTranslator->get('ERR_RESULT').$sendErr.'</ul>';
    		}
    	}

    	return '';
    }

    private function register () {
    	$model = $this->model;

    			$role = new Role();
    			$role->setRoleId(2);
    			$role->setType("User");

    			$addressDelivery = new Address();
    	//		$addressDelivery->setAddressId(1);
    			$addressDelivery->setCity($model->city);
//    			$addressDelivery->setCountry($model->country);
    			$addressDelivery->setCountryCode($model->country);
    			$addressDelivery->setPlz($model->zip);
    			$addressDelivery->setStreet($model->street);
    			$addressDelivery->setTelephone($model->phone);


    			$user = new User();
    			//$user->setUserId(2);
	    		if ($model->useBillingAddress == true) {
	    			$addressBill = new Address();
	    			//$addressBill->setAddressId(4);
	    			$addressBill->setCity($model->cityBilling);
	    			$addressBill->setCountryCode($model->countryBilling);
	    			$addressBill->setPlz($model->zipBilling);
	    			$addressBill->setStreet($model->streetBilling);
	    			$addressBill->setTelephone($model->phoneBilling);

	    			$user->setBillAddress($addressBill);
	    		}

    			$user->setDeliveryAddress($addressDelivery);
    			$user->setEmail($model->email);
    			$user->setFirstname($model->firstName);
    			$user->setLastname($model->lastName);
    			$user->setNewsletter(1);
    			$user->setPassword($model->password);
    			$user->setRole($role);
    			$user->setSessionId($model->sessionId);
    			$user->setStatus("inactive");
    			$user->setSubscription(NULL);

    			$manager = new AdminRole_UserManager();
    			return $manager->addUser($user, true);
    			/*case 2 - edit user*/
    			//$manager->editUser($user, true, true);
    			/*case 3 - get all user*/
//     			$users = $manager->getAll($testArrayFilter, $testArraySort, true);
// 	    		while ($row = $users->getNext()) {
// 					print $row->Vorname;
// 				}
				/*case 4*/
	}


}

?>